package com.junxonline.plat.controller.attence;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.attence.AttenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: AttenceController
 * @Description: 考勤控制器
 */

@RestController
@RequestMapping(value = "/admin/attence")
public class AttenceController extends BaseController {

    @Autowired
    private AttenceService attenceService;
    
    /**
     * 获取考勤数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getAttence")
    public ResultVO getAttence(@RequestBody QueryAttenceVO vo) {

        if (CommonUtils.isEmpty(vo.getAttenceId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(attenceService.getAttence(vo));
    }

    /**
     * 筛选查询考勤数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryAttence")
    public PageResultVO queryAttence(PageVO page, QueryAttenceVO vo) {
        return getReturnSuccess(attenceService.queryAttence(page, vo));
    }


    /**
     * 添加考勤
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addAttence")
    public ResultVO addAttence(@RequestBody AttenceVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseholdId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = attenceService.addAttence(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改考勤
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editAttence")
    public ResultVO editAttence(@RequestBody AttenceVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getAttenceId()) || CommonUtils.isEmpty(vo.getHouseholdId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = attenceService.editAttence(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除考勤
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delAttence")
    public ResultVO delAttence(@RequestBody AttenceIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] attenceIdsArray = vo.getAttenceIds();

        boolean successMenu = attenceService.delAttence(attenceIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 获取员工数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getPersonnel")
    public ResultVO getHousehold(@RequestBody QueryHouseholdVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseholdName())) {
            return getReturnSuccess();
        }

        return getReturnSuccess(attenceService.getHousehold(vo));
    }

}
